package network;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpClient {
    private Socket socket = null;
    public TcpClient(String serverIP,int serverPort) throws IOException {
        //端口号和ip地址
        socket = new Socket(serverIP,serverPort);
    }

    public void start() throws Exception {
        Scanner scanner = new Scanner(System.in);
        try (InputStream inputStream = socket.getInputStream(); OutputStream outputStream = socket.getOutputStream()){
            PrintWriter writer = new PrintWriter(outputStream);
            Scanner scannerNetwork = new Scanner(inputStream);
            while (true){
                System.out.print("-->");
                StringBuffer str = new StringBuffer("1-");
                System.out.print("请输入账号:");
                String s = scanner.nextLine();
                str.append(s);
                String request = str.toString();
                writer.println(request);
                writer.flush();
                String response = scannerNetwork.next();
                System.out.println(response);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        //TcpClient client = new TcpClient("127.0.0.1",9090);
        TcpClient client = new TcpClient("124.221.111.81",9090);
        client.start();
    }
}
